package com.cyanite.playlv2;

import com.cyanite.playlv2.widget.SearchCategoryAdapter;
import com.cyanite.playlv2.widget.SegmentedButton;
import com.cyanite.playlv2.widget.SegmentedButton.OnClickListenerSegmentedButton;
import com.cyanite.playlv2api.types.VenueCategory;
import com.cyanite.playlv2api.types.Group;
import com.cyanite.playlv2api.util.ConstantsUtils;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.widget.AdapterView.OnItemClickListener;

public class SearchDialog extends Dialog   {

    private ViewFlipper mViewFlipper;
    private SearchCategoryAdapter mSearchCategoryAdapter;
    private EditText mQuery;
    private Context mContext;
    private View mViewTripCategory;
    private View mViewVenueCategory;
    private SegmentedButton mHeaderButton;
    private boolean mIsTrip;

    public SearchDialog(Context context) {
        super(context);
        mContext = context;
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.search_category_dialog);
        setTitle(mContext.getString(R.string.search_dialog_title));

        ensureUi();
    }

    private void ensureUi(){

        mQuery = (EditText)findViewById(R.id.etSearchContent);
        mQuery.setHint(R.string.trip_search_line);

        mHeaderButton = (SegmentedButton)findViewById(R.id.btnSearchSegmented);
        mHeaderButton.addButtons(
                mContext.getString(R.string.search_type_trip),
                mContext.getString(R.string.search_type_task));

        mHeaderButton.setOnClickListener(new OnClickListenerSegmentedButton() {
            public void onClick(int index) {
                mViewFlipper.removeAllViews();
                switch(index)   {
                    case 0:
                        mIsTrip = true;
                        mQuery.setHint(R.string.trip_search_line);
                        mViewFlipper.addView(mViewTripCategory);
                        break;
                    case 1:
                        mIsTrip = false;
                        mQuery.setHint(R.string.trip_search_task);
                        mViewFlipper.addView(mViewVenueCategory);
                        break;
                    default:
                        break;
                }
            }
        });


        ImageView btnSearch = (ImageView)findViewById(R.id.imgSearch);
        btnSearch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String query = mQuery.getText().toString();
                if (!query.equals(""))  {
                    Intent intent = new Intent(mContext, SearchResultActivity.class);
                    intent.putExtra(SearchResultActivity.EXTRA_SEARCH_TRIP, mIsTrip);
                    intent.putExtra(SearchResultActivity.EXTRA_SEARCH_QUERY, query);
                    mContext.startActivity(intent);

                }   else {
                    Toast.makeText(mContext,
                            mContext.getResources().getString(R.string.search_dialog_query_null),
                            Toast.LENGTH_LONG).show();
                }
            }
        });

        mViewFlipper = (ViewFlipper) findViewById(R.id.categorySearchViewFlipper);
        try {
            /* no need to fetch anymore
            DataBaseAdapter db = new DataBaseAdapter(mContext);
            db.open();
            Group<VenueCategory> categories = new Group<VenueCategory>();
            db.getSearchCategories(categories);
            db.close();
            */
            mViewTripCategory = tripsCategoryPage(mContext);
            mViewVenueCategory = venuesCategoryPage(mContext, ConstantsUtils.getSearchTaskCategories());
            mViewFlipper.addView(mViewTripCategory);
            mIsTrip = true;

        }   catch (Exception ex)    {
        }
    }

    @Override
    protected void onStop() {
        super.onStop();

    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mViewFlipper.getChildCount() > 0) {
                    if(mViewFlipper.getChildCount() == 1){
                        this.cancel();
                    }
                    mViewFlipper.removeViewAt(mViewFlipper.getChildCount() - 1);
                    return true;
                }
                break;
        }

        return super.onKeyDown(keyCode, event);
    }

    private View venuesCategoryPage(Context context, Group<VenueCategory> categories) {

        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.search_category_page, null);
        mSearchCategoryAdapter = new SearchCategoryAdapter(context, categories);
        ListView listview = (ListView) view.findViewById(R.id.lvCategoryListView);
        listview.setAdapter(mSearchCategoryAdapter);
        listview.setOnItemClickListener(mOnTripListItemClickListener);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 300));
        return view;
    }

    private View tripsCategoryPage(Context context)  {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View view = inflater.inflate(R.layout.search_category_page, null);
        mSearchCategoryAdapter = new SearchCategoryAdapter(context, ConstantsUtils.getTripsCategories());
        ListView listview = (ListView) view.findViewById(R.id.lvCategoryListView);
        listview.setAdapter(mSearchCategoryAdapter);
        listview.setOnItemClickListener(mOnTripListItemClickListener);
        view.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 300));
        return view;
    }

    private OnItemClickListener mOnTripListItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            String query = mQuery.getText().toString();
            if (!query.equals(""))  {
                Intent intent = new Intent(mContext, SearchResultActivity.class);
                intent.putExtra(SearchResultActivity.EXTRA_SEARCH_TRIP, mIsTrip);
                intent.putExtra(SearchResultActivity.EXTRA_SEARCH_QUERY, query);
                intent.putExtra(SearchResultActivity.EXTRA_SEARCH_POSITON, position);
                mContext.startActivity(intent);
            }   else {
                Toast.makeText(mContext,
                        mContext.getResources().getString(R.string.search_dialog_query_null),
                        Toast.LENGTH_LONG).show();
            }
        }
    };
}